<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\RecordModel;
use app\model\UserModel;
use think\Request;

class Record extends Base
{
//    获取当前登录用户的全部记录本
    public function getMyRecordInfo(Request $request){

        $user_id=UserModel::getUseridByToken($request->param(('token'))); //获取用户id

        $db=new RecordModel();

        $info = $db->table('record')
            ->where('user_id',$user_id)
            ->order('record_time','desc')
            ->select();

        $total=$db->table('record')
            ->where('user_id',$user_id)
            ->order('record_time','desc')
            ->count('user_id');

        return json(['code'=>1,'data'=>$info,'total'=>$total]);



    }
//    添加记录本
    public function addRecord(Request $request){

        $user_id=UserModel::getUseridByToken($request->param(('token'))); //获取用户id
        $record_info=$request->param(('addRecordForm'));

        $record=new RecordModel();
        $record_info['user_id']=$user_id;

        if($record->insert($record_info)){
            return json(['code'=>1]);

        }
        return json(['code'=>0]);
    }


//    根据记录本id修改记录本
    public function updateRecord(Request $request){


        $record_id= $request->param('record_id');
        $record_title=$request->param('record_title');
        $record_content=$request->param('record_content');

        $record= RecordModel::find($record_id);

        $data=['record_title'=>$record_title,'record_content'=>$record_content];

        if($record->save($data)){

            return json(['code'=>1]);
        }
        return json(['code'=>0]);

    }
    //    根据记录本id删除记录本
    public function deleteRecord(Request $request){

        $record_id= $request->param('record_id');

        $record = RecordModel::find($record_id);

        if($record!=Null){
            if($record->delete()){
                return json(['code'=>1]);
            }
        }

        return json(['code'=>0]);
    }

    //    修改记录本权限
    public function updateAuthority(Request $request){
        $record_id= $request->param('record_id');
        $record_authority= $request->param('record_authority');

        $record=RecordModel::find($record_id);
        if($record_authority==1){  //公开转私密

            $record->record_authority =0;
            if($record->save()){
                return json(['code'=>1]);
            }
        }else{
            //私密转公开
            $record->record_authority =1;
            if($record->save()){
                return json(['code'=>1]);
            }
        }

        return json(['code'=>0]);
    }

//    根据record_id获取记录本信息
    public function getRecordByID(Request $request){

        $record_id= $request->param('record_id');
        $record=RecordModel::find($record_id);

        return json(['data'=>$record]);
    }

//    获取用户公开的记录本
    public function getUserRecordInfo(Request $request){

        $user_id=$request->param(('user_id')); //获取用户id

        $db=new RecordModel();

        $info = $db->table('record')
            ->where('user_id',$user_id)
            ->where('record_authority',1)
            ->order('record_time','desc')
            ->select();

        $total=$db->table('record')
            ->where('user_id',$user_id)
            ->where('record_authority',1)
            ->order('record_time','desc')
            ->count('user_id');

        return json(['code'=>1,'data'=>$info,'total'=>$total]);



    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
